/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.ICharStoChoMeterDao;
import com.ce.pms.charge.model.CharStaChoDomain;
import com.ce.pms.charge.vo.CharStaChoMeterVO;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：房间标准选用 收费类型为走表的持久层<br>
 * @author 罗军林 创建日期：2008-8-7
 */
public class CharStoChoMeterDaoImpl extends PmsBaseDaoImpl<CharStaChoDomain> implements ICharStoChoMeterDao{

	public Page queryCharStoCho(ChargeStaChoVO condition) {
		Map<String, Object> parameter = new HashMap<String, Object>();

		StringBuffer sql = new StringBuffer("SELECT charstacho  FROM CharStaChoDomain charstacho,FlatDomain flat WHERE charstacho.house.id=flat.id ");
		sql.append(buildCondition(condition,parameter));
		sql.append(buildOrder(condition));
		return this.pagedNameQueryByPageNo(sql.toString(), condition.getPageNo(), condition.getPageSize(),parameter);
	}

	/**
	 * 功能描述:查找指定房号ID和走表ID的标准选用<br>
	 * @param houseId
	 * @param meterId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-9<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<CharStaChoDomain> queryCharStoCho(Long houseId,Long meterId){
		String hql="FROM CharStaChoDomain cho WHERE cho.house.keyword=? and cho.meter.keyword=?";
		Query query=createQuery(hql, houseId,meterId);
		return query.list();
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoMeterDao#addCharStaChoMeter(com.ce.pms.charge.vo.CharStaChoMeterVO)
	 */
	public void addCharStaChoMeter(CharStaChoMeterVO charStaChoMeterVO){
		String sql="INSERT INTO FEE_CHRG_STA_CHO(HOUSE_ID, CHARGE_ITEM_ID, CHARGE_STA_ID, METER_ID, CREATOR, SYSDATE)" +
				" VALUES(?,?,?,(select METER_ID from HM_METER where METER_TYPE_ID=? and HOUSE_ID=? and VALIDITY=1),?,now()) ";

		List<Object[]> list=new ArrayList<Object[]>();
		String[] houseIds=charStaChoMeterVO.getHouseIds().split(",");
		for(int i=0;i<houseIds.length;i++){
			Object[] args={Long.valueOf(houseIds[i]),
					charStaChoMeterVO.getChargeItemID(),
					charStaChoMeterVO.getChargeStaID(),
					charStaChoMeterVO.getMeterTypeId(),
					Long.valueOf(houseIds[i]),
					charStaChoMeterVO.getCreater()};
			list.add(args);

			if( (i+1) % 50 ==0 || i==(houseIds.length-1)){
				this.executeSQLBatch(sql, list);
				list.clear();
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoDao#checkExistCharStoChoMeter
	 * (com.ce.pms.charge.vo.CharStaChoMeterVO)
	 */
	public Boolean checkExistCharStoChoMeter(CharStaChoMeterVO searchCondition,CharStaChoMeterVO oldCondition){
		Map<String, Long> parameter = new HashMap<String, Long>();

		Long meterId=queryMeterId(searchCondition.getHouseId(),searchCondition.getMeterTypeId());
		if(meterId==null){
			return true;
		}

		StringBuffer sql=new StringBuffer("SELECT COUNT(*) FROM CharStaChoDomain char ")
		.append(" WHERE 1=1 ")
		.append(builderParameter(searchCondition,oldCondition,meterId,parameter));

		Query query=this.createNameQuery(sql.toString(), parameter);
		Long result=(Long)query.uniqueResult();

		return result>0;
	}

	/**
	 * 方法描述: 根据表ID和房屋ID查询相应的房间标准选用记录
	 * @author 胡煦
	 * Create on:2008-10-8
	 * @param nameStr：查询条件班级名称
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<CharStaChoDomain> queryCharStaChoDoBy(Long houseId,Long meterId){
		String hql = "from CharStaChoDomain cd where cd.house.id = ? and meter.id = ?";
		Query query = createQuery(hql, new Object[]{houseId,meterId});
		return query.list()==null?null:query.list();
	}


	/** 功能描述:<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Long queryMeterId(Long hid,Long meterTypeid) {
		String sql="SELECT meter.keyword FROM MeterDomain meter " +
					"WHERE meter.meterType.keyword=? AND meter.validity=1 " +
					"AND meter.house.keyword=?";
		Query query1=this.createQuery(sql, meterTypeid,hid);
		Object meterId=query1.uniqueResult();
		if(meterId==null){
			return null;
		}else{
			return (Long)meterId;
		}
	}

	/**
	 * 功能描述:构建唯一验证的查询条件<br>
	 * @param charStaChoMeterVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String  builderParameter(CharStaChoMeterVO searchCondition,CharStaChoMeterVO oldCondition,Long meterId,Map<String, Long> parameter) {
		StringBuffer result=new StringBuffer();
		if(searchCondition!=null && searchCondition.getHouseId()!=null){
			parameter.put("hid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword=:hid ");
		}
		if(searchCondition!=null && searchCondition.getChargeItemID()!=null){
			parameter.put("itemid", searchCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword=:itemid ");
		}
		if(searchCondition!=null && searchCondition.getMeterId()!=null){
			parameter.put("meterTypeId", meterId);
			result.append(" AND char.meter.keyword=:meterTypeId");
		}

		if(oldCondition!=null && oldCondition.getHouseId()!=null){
			parameter.put("oldHid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword<>:oldHid ");
		}
		if(oldCondition!=null && oldCondition.getChargeItemID()!=null){
			parameter.put("oldItemid", oldCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword<>:oldItemid ");
		}
		if(oldCondition!=null && oldCondition.getMeterId()!=null){
			parameter.put("oldMeterid", oldCondition.getMeterId());
			result.append(" AND char.meter.keyword<>:oldMeterid ");
		}
		return result.toString();
	}

	/**
	 * 功能描述:构建查询条件<br>
	 * @param condition
	 * @param parameter
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildCondition(ChargeStaChoVO condition,Map<String,Object> parameter){
		StringBuffer result=new StringBuffer();
		if(condition == null){
			return "";
		}

		result.append(" AND charstacho.chargeItem.chrgType=:chrgType ");
		parameter.put("chrgType", SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER);

		if(condition.getBuildingID() != null){
			result.append(" AND charstacho.house.building.keyword = :buildingID ");
			parameter.put("buildingID", condition.getBuildingID() );
		}

		if(condition.getChargeItemID() != null){
			result.append(" AND charstacho.chargeItem.keyword = :chargeItemID" );
			parameter.put("chargeItemID", condition.getChargeItemID() );
		}

		if(condition.getChargeStaID() != null){
			result.append(" AND charstacho.chargeSta.keyword = :chargeStaID ");
			parameter.put("chargeStaID", condition.getChargeStaID() );
		}

		if(condition.getHouseNumber() != null && condition.getHouseNumber().trim() != ""){
			result.append(" AND charstacho.house.houseNumber like :houseNumber ");
			parameter.put("houseNumber", "%"+condition.getHouseNumber()+"%");
		}

		return result.toString();
	}

	/**
	 * 功能描述:构建排序条件<br>
	 * @param condition
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildOrder(ChargeStaChoVO condition){
		if(condition.getSortStr()==null || condition.getSortStr().trim().length()<1){
			return " ORDER BY charstacho.house.building.seqNo,flat.floor,charstacho.house.houseNumber ";
		}else if("SYSDATE".indexOf(condition.getSortStr().trim().split(" ")[0].toUpperCase())>=0){
			return " ORDER BY charstacho.sysInfo.sysDate DESC" ;
		}else{
			return " ORDER BY " + condition.getSortStr() ;
		}
	}
}
